/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.wx.entity.WxActivity;
import com.jeesite.modules.wx.entity.WxUser;
import com.jeesite.modules.wx.service.WxActivityService;
import com.jeesite.modules.wx.service.WxUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.wx.entity.WxUserDisValueRecord;
import com.jeesite.modules.wx.service.WxUserDisValueRecordService;

import java.util.List;

/**
 * wx_user_dis_value_recordController
 * @author wangjianxin
 * @version 2018-10-02
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxUserDisValueRecord")
public class WxUserDisValueRecordController extends BaseController {

	@Autowired
	private WxUserDisValueRecordService wxUserDisValueRecordService;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private WxActivityService wxActivityService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WxUserDisValueRecord get(String id, boolean isNewRecord) {
		return wxUserDisValueRecordService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxUserDisValueRecord wxUserDisValueRecord, Model model) {
		model.addAttribute("wxUserDisValueRecord", wxUserDisValueRecord);
		return "modules/wx/wxUserDisValueRecordList";
	}

	/**
	 * 推广明细
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "wxDisSpreadList")
	public String wxDisSpreadList(WxUserDisValueRecord wxUserDisValueRecord, Model model,
								  @RequestParam(value = "userId",required = false) String userId,
								  @RequestParam(value = "buyActivityId",required = false) String buyActivityId) {

		logger.info("查看推广人={}",userId);
		logger.info("查看推广活动={}",buyActivityId);

		model.addAttribute("wxUserDisValueRecord", wxUserDisValueRecord);
		model.addAttribute("userId", userId);
		model.addAttribute("buyActivityId", buyActivityId);
		return "modules/wx/wxDisSpreadList";
	}

	/**
	 * 我的上级，就是扫了谁的码
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "wxParentDisList")
	public String wxParentDis(WxUserDisValueRecord wxUserDisValueRecord, Model model,
								  @RequestParam(value = "userId",required = false) String userId,
								  @RequestParam(value = "buyActivityId",required = false) String buyActivityId) {

		logger.info("查看推广人={}",wxUserDisValueRecord.getBuyUserId());
		logger.info("查看推广活动={}",buyActivityId);

		model.addAttribute("wxUserDisValueRecord", wxUserDisValueRecord);
		model.addAttribute("buyUserId", wxUserDisValueRecord.getBuyUserId());
		model.addAttribute("buyActivityId", buyActivityId);
		return "modules/wx/wxParentDisList";
	}

	/**
	 * 扫了谁的码明细
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "parentDisListData")
	@ResponseBody
	public Page<WxUserDisValueRecord> parentDisListData(WxUserDisValueRecord wxUserDisValueRecord, HttpServletRequest request, HttpServletResponse response) {

		logger.info("查看用户={}",wxUserDisValueRecord.getBuyUserId());
		logger.info("查看用户购买活动={}",wxUserDisValueRecord.getBuyActivityId());

		//购买状态
		wxUserDisValueRecord.setBuyFlag(1);
		Page<WxUserDisValueRecord> page = wxUserDisValueRecordService.findPage(new Page<WxUserDisValueRecord>(request, response), wxUserDisValueRecord);
		List<WxUserDisValueRecord> wxUserDisValueRecordList = page.getList();
		if(CollectionUtils.isNotEmpty(wxUserDisValueRecordList)){
			for(WxUserDisValueRecord temp : wxUserDisValueRecordList){
				//查询该活动下购买的用户详细信息
				if(null == temp.getUserId()){
					continue;
				}
				String userId = temp.getUserId().toString();
				WxUser wxUser = wxUserService.get(userId);
				temp.setWxUser(wxUser);
				temp.setNickName(wxUser.getNickName());
				temp.setUserName(wxUser.getUserName());
				temp.setHeadImg(wxUser.getHeadImg());
				temp.setPhone(wxUser.getPhone());

				//查询活动详情信息
				String activityId = temp.getBuyActivityId().toString();
				WxActivity findIdByActivity = wxActivityService.get(activityId);
				temp.setWxActivity(findIdByActivity);
				temp.setActivityTitle(findIdByActivity.getTitle());
				temp.setActivityPrice(temp.getBuyPrice().toString());
				if(temp.getBuyFlag() == 0){
					temp.setBuyFlagContent("参与用户(未购买)");
				}else{
					temp.setBuyFlagContent("已购买");
				}
			}

		}
		return page;
	}


	/**
	 * 推广明细查询
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "disDetailListData")
	@ResponseBody
	public Page<WxUserDisValueRecord> disDetailListData(WxUserDisValueRecord wxUserDisValueRecord, HttpServletRequest request, HttpServletResponse response) {

		logger.info("查看推广人={}",wxUserDisValueRecord.getUserId());
		logger.info("查看推广活动={}",wxUserDisValueRecord.getBuyActivityId());

		//购买状态
		wxUserDisValueRecord.setBuyFlag(1);
		Page<WxUserDisValueRecord> page = wxUserDisValueRecordService.findPage(new Page<WxUserDisValueRecord>(request, response), wxUserDisValueRecord);
		List<WxUserDisValueRecord> wxUserDisValueRecordList = page.getList();
		if(CollectionUtils.isNotEmpty(wxUserDisValueRecordList)){
			for(WxUserDisValueRecord temp : wxUserDisValueRecordList){
				//查询该活动下购买的用户详细信息
				String userId = temp.getBuyUserId().toString();
				WxUser wxUser = wxUserService.get(userId);
				temp.setWxUser(wxUser);
				temp.setNickName(wxUser.getNickName());
				temp.setUserName(wxUser.getUserName());
				temp.setHeadImg(wxUser.getHeadImg());
				temp.setPhone(wxUser.getPhone());

				//查询活动详情信息
				String activityId = temp.getBuyActivityId().toString();
				WxActivity findIdByActivity = wxActivityService.get(activityId);
				temp.setWxActivity(findIdByActivity);
				temp.setActivityTitle(findIdByActivity.getTitle());
				temp.setActivityPrice(temp.getBuyPrice().toString());
				if(temp.getBuyFlag() == 0){
					temp.setBuyFlagContent("参与用户(未购买)");
				}else{
					temp.setBuyFlagContent("已购买");
				}
			}

		}
		return page;
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WxUserDisValueRecord> listData(WxUserDisValueRecord wxUserDisValueRecord, HttpServletRequest request, HttpServletResponse response) {
		//购买状态，全部用户
		if(wxUserDisValueRecord.getBuyFlag() == 2){
			wxUserDisValueRecord.setBuyFlag(null);
		}
		Page<WxUserDisValueRecord> page = wxUserDisValueRecordService.findPage(new Page<WxUserDisValueRecord>(request, response), wxUserDisValueRecord);
		List<WxUserDisValueRecord> wxUserDisValueRecordList = page.getList();


		if(CollectionUtils.isNotEmpty(wxUserDisValueRecordList)){

			for(WxUserDisValueRecord temp : wxUserDisValueRecordList){

				//查询该活动下购买的用户详细信息
				String userId = temp.getBuyUserId().toString();
				WxUser wxUser = wxUserService.get(userId);
				temp.setWxUser(wxUser);
				temp.setNickName(wxUser.getNickName());
				temp.setUserName(wxUser.getUserName());
				temp.setHeadImg(wxUser.getHeadImg());
				temp.setPhone(wxUser.getPhone());

				//查询活动详情信息
				String activityId = temp.getBuyActivityId().toString();
				WxActivity findIdByActivity = wxActivityService.get(activityId);
				temp.setWxActivity(findIdByActivity);
				temp.setActivityTitle(findIdByActivity.getTitle());
				temp.setActivityPrice(temp.getBuyPrice().toString());
				if(temp.getBuyFlag() == 0){
					temp.setBuyFlagContent("参与用户(未购买)");
				}else{
					temp.setBuyFlagContent("购买用户");
				}
			}

		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:view")
	@RequestMapping(value = "form")
	public String form(WxUserDisValueRecord wxUserDisValueRecord, Model model) {
		model.addAttribute("wxUserDisValueRecord", wxUserDisValueRecord);
		return "modules/wx/wxUserDisValueRecordForm";
	}

	/**
	 * 保存wx_user_dis_value_record
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WxUserDisValueRecord wxUserDisValueRecord) {
		wxUserDisValueRecordService.save(wxUserDisValueRecord);
		return renderResult(Global.TRUE, text("保存wx_user_dis_value_record成功！"));
	}
	
	/**
	 * 删除wx_user_dis_value_record
	 */
	@RequiresPermissions("wx:wxUserDisValueRecord:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WxUserDisValueRecord wxUserDisValueRecord) {
		wxUserDisValueRecordService.delete(wxUserDisValueRecord);
		return renderResult(Global.TRUE, text("删除wx_user_dis_value_record成功！"));
	}
	
}